package com.example.test.controller;


import com.example.test.bean.BraendType;
import com.example.test.bean.DTO.input.UsersCreateInputDTO;
import com.example.test.bean.DTO.input.UsersUpdateInputDTO;
import com.example.test.common.model.PageModel;
import com.example.test.common.model.R;
import com.example.test.service.UsersService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * <p>
 * 管理员信息表 前端控制器
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@CrossOrigin
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response,
                        String username, String password) {
        System.out.println("用户发起登录请求。。。。");
        //获取登录状态
        String result = (String) request.getAttribute("verification result");
        if("logged in".equals(result)) {//校验用户是否已经登录
            System.out.println("用户已登录");
            return "already logined";
        }
        if(!"123".equals(username)) {//校验用户名称
            System.out.println("用户不存在");
            return "用户不存在";
        }
        if(!"123".equals(password)) {//校验用户密码
            System.out.println("账号或密码错误");
            return "账号或密码错误";
        }
        //生成一个token
        String token = UUID.randomUUID().toString();
        System.out.println("用户登录成功，获得token:" + token);
        HttpSession session = request.getSession();
        //服务端备份token
        session.setAttribute("token", token);
        //返回token
        return token;
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request,HttpServletResponse response) {

        System.out.println("用户登出");
        //获取登录校验结果
        String result = (String) request.getAttribute("verification result");

        if(!"logged in".equals(result)) {//如果未通过登录校验
            return "用户未登录或token错误，无法登出";
        }

        /*通过登录校验，登出用户*/
        HttpSession session = request.getSession();

        //清空token
        session.setAttribute("token", null);

        System.out.println("登出成功");
        return "登出成功";
    }

    //查看登录状态
    @RequestMapping("loginStatus")
    public String loginStatus(HttpServletRequest request,HttpServletResponse response) {

        System.out.println("获取用户登录状态");

        //获取登录校验结果
        String token = (String) request.getAttribute("verification result");

        if("logged in".equals(token)) {
            return "用户已登录";
        }

        if("wrong token".equals(token)) {
            return "口令错误或超时";
        }

        if("not logged in".equals(token)) {
            return "用户未登录";
        }
        return "未知错误";
    }

    @ApiOperation(value = "商品信息表根据keyword分页查询")
    @GetMapping("/getByKeyword")
    public R usersList( String keyword, PageModel pageModel) {
        return usersService.getTypeByKeyword(keyword,pageModel);
    }

    @ApiOperation(value = "根据id分页查询")
    @GetMapping("/getUser")
    public R listById( Long userId) {
        return usersService.getUserById(userId);
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/insert")
    public R insert(  UsersCreateInputDTO usersCreateInputDTO) {
        return usersService.addUser(usersCreateInputDTO);
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public R update(@Validated(BraendType.class) @ApiParam UsersUpdateInputDTO usersUpdateInputDTO) {
        return usersService.updateUser(usersUpdateInputDTO);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete")
    public R<Boolean> delete(@Validated @ApiParam  Long userId) {
        return usersService.deleteUser(userId);
    }

}

